package com.androider.timememory.activity;

import java.lang.ref.WeakReference;

import com.androider.timememory.R;
import com.androider.timememory.db.Record;
import com.androider.timememory.db.RecordModel;
import com.androider.timememory.utils.ActivityUtil;
import com.androider.timememory.utils.Constants;
import com.androider.timememory.utils.FileUtil;
import com.androider.timememory.utils.ImageUtil;
import com.androider.timememory.utils.RecordUtil;
import com.androider.timememory.utils.StringUtil;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.media.ThumbnailUtils;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

public class NewPicture extends Activity implements OnClickListener {
	private Context mContext;
	private String picPath;
	private String picName;
	
	private WeakReference<Bitmap> thumbnail;
	private Bitmap mBitmap;
	private Bitmap bmp;
	
	private ImageView picture;
	private TextView titleDate;
	private Button positive;
	private Button negative;
	private EditText note;
	private CheckBox categoryFamily;
    private CheckBox categoryLover;
    private CheckBox categoryFriend;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.new_picture);
		mContext = NewPicture.this;
		setViews();
		
		Intent intent = this.getIntent();
		if(intent != null) {
			picPath = intent.getStringExtra("picture");
			picName = String.valueOf(intent.getLongExtra("picName", 0));
			bmp = BitmapFactory.decodeFile(picPath);
			picture.setImageBitmap(new WeakReference<Bitmap>(bmp).get());
			new Thread() {
				@Override
				public void run() {
					//save bitmap thumbnail
					mBitmap = ThumbnailUtils.extractThumbnail(BitmapFactory.decodeFile(picPath), 300, 200, ThumbnailUtils.OPTIONS_RECYCLE_INPUT);
					thumbnail = new WeakReference<Bitmap>(mBitmap);
					ImageUtil.saveBitmap(FileUtil.RECORD_PICTURE_THUMBNAIL_DIR, picName, thumbnail.get());
				}
			}.start();
		}
		
//		InputMethodManager imm;
//		imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
//		imm.hideSoftInputFromWindow(note.getWindowToken(), 0);
	}
	
	private void setViews() {
		picture = (ImageView) findViewById(R.id.picture);
		positive = (Button) findViewById(R.id.positive);
		negative = (Button) findViewById(R.id.negative);
		note = (EditText) findViewById(R.id.note);
		categoryFamily = (CheckBox) findViewById(R.id.category_family);
        categoryLover = (CheckBox) findViewById(R.id.category_lover);
        categoryFriend = (CheckBox) findViewById(R.id.category_friend);
        titleDate = (TextView) findViewById(R.id.title_date);
        findViewById(R.id.search).setVisibility(View.GONE);
        titleDate.setText(StringUtil.getCurrnetDate("yyyy年MM月dd日"));
        
        positive.setOnClickListener(this);
        negative.setOnClickListener(this);
	}

	@Override
	public void onClick(View v) {
		int id = v.getId();
		switch(id) {
			case R.id.positive:
				String category = RecordUtil.getCategory(categoryFamily, categoryLover, categoryFriend);
				if(null == category || category.equals("")) {
					ActivityUtil.showShortTip(mContext, R.string.tip_category_not_selected);
					return;
				}
				
				Record record = new Record();
				record.setCategory(category.toString());
				record.setNote(note.getText().toString());
				record.setDate(StringUtil.getCurrnetDate("yyyy.MM.dd HH:mm:ss"));
				record.setType(Record.TYPE_PHOTO);
				record.setContent(picName);
				
				boolean flag = RecordModel.insertRecord(mContext, record);
				if(flag) {
					ActivityUtil.showShortTip(mContext, R.string.tip_picture_add_success);
				} else {
					ActivityUtil.showShortTip(mContext, R.string.tip_picture_add_failure);
				}
				
				//refresh data list
				this.sendBroadcast(new Intent(Constants.ACTION_RECORD_ADD));
				
				NewPicture.this.finish();
				break;
			case R.id.negative:
				NewPicture.this.finish();
				break;
		}
		
	}

	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		if(thumbnail != null) {
			thumbnail.clear();
		}
		if(mBitmap != null) {
			mBitmap.recycle();
		}
		if(bmp != null) {
			bmp.recycle();
		}
		super.onDestroy();
	}
	
}
